package br.com.felix.clearcase.command;

import br.com.felix.easyclearcase.EasyClearCaseFile;
import br.com.felix.easyclearcase.EasyClearCaseFile.ClearCaseStatus;

/**
 * This class encapsulates the cleartool command to add a file/folder to source control.
 * Parent folder must be checked out.
 * @see ClearToolCommand
 * */
public class AddToSourceControl extends ClearToolCommand {
	
	private String comment;
	
	/**
	 * Instantiates a new command.
	 * @param fileInVob a file/folder that is not under source control, but it is already copied into the vob network directory.
	 * @param comment the comment that will be registered with clearcase.
	 * */
	public AddToSourceControl(EasyClearCaseFile fileInVob, String comment){
		this.file = fileInVob;
		this.comment = comment;
		setWorkingDirectory(fileInVob.getParentFile());
	}
	
	
	/**
	 * @see ClearToolCommand#toCommandLine()
	 * */
	@Override
	public String toCommandLine() {
		comment.replaceAll("\\$\\{file\\}", file.getName());
		if(file.isDirectory()){
			return "cleartool mkelem -co \""+comment+"\" \"" + file.getName()+"\"";
		}
		else{
			return "cleartool mkelem -ci -ptime -co \""+comment+"\" \"" + file.getName()+"\"";
		}
	}


	@Override
	public String getCommandName() {
		return "Add to Source Control";
	}


	@Override
	public ClearCaseStatus getStatusAfterExecution() {
		if(file.isDirectory()){
			return ClearCaseStatus.CHECKEDOUT;
		}
		else{
			return ClearCaseStatus.FILE_ELEMENT;
		}
	}


	@Override
	public boolean shouldExecute() {
		if(file!=null){
			return file.getStatus() == ClearCaseStatus.PRIVATE_FILE || file.getStatus() == ClearCaseStatus.UNKNOWN;
		}
		return true;
	}
	
}
